package com.mys2sh.action;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.mys2sh.cookie.UserCookie;
import com.mys2sh.model.User;
import com.mys2sh.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
	private UserService userService;
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		
		if (user == null
				|| !userService.checkValidUser(user.getUserName(), user
						.getPassWord())) {
			addActionError("用户名或密码不正确！");
			return INPUT;
		}

		User loginUser = userService.getUserInfoByUserName(user.getUserName());

		UserCookie.setLoginCookie(loginUser, response);

		return SUCCESS;
	}

}
